package com.youdoneed.common.controller;

import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.base.common.JWTUtils;
import com.youdoneed.base.common.RSAEncryptor;
import com.youdoneed.base.common.RsaKeySalt;
import com.youdoneed.common.model.CheckImageResult;
import com.youdoneed.common.service.CheckImage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Ewing
 * @since 2017-04-21
 **/
@RestController
@RequestMapping("/public")
@Api(value = "公共通用方法", description = "公共通用方法")
public class PublicController {

    /**
     * 获取服务器毫秒时间。
     */
    @ApiOperation("获取服务器毫秒时间")
    @RequestMapping(value = "/timeMillis", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<Long> timeMillis() {
        return new ResultMessage<>(System.currentTimeMillis());
    }

    /**
     * 获取动态密钥及加密盐。
     */
    @ApiOperation("获取动态密钥及加密盐")
    @RequestMapping(value = "/rsaKeySalt", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<RsaKeySalt> rsaKeySalt() {
        RsaKeySalt rsaKeySalt = RSAEncryptor.getPublicKeySalt();
        return new ResultMessage<>(rsaKeySalt);
    }

    /**
     * 更新Token。
     */
    @ApiOperation("更新Token，新token有效期一个月")
    @RequestMapping(value = "/refreshToken", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<String> refreshToken(@RequestHeader("Authorization") String token) {
        token = JWTUtils.putClaimsData(token, "exp", JWTUtils.oneMonthExp());
        return new ResultMessage<>(token);
    }

    /**
     * 图片鉴黄鉴暴。
     */
    @ApiOperation("图片鉴黄鉴暴")
    @RequestMapping(value = "/checkImage", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<CheckImageResult> checkImage(String imgUrl) {
        return new ResultMessage<>(CheckImage.checkImage(imgUrl));
    }

}
